package com.eight.qianTeacher.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.eight.common.pojo.EduChapter;
import com.eight.common.pojo.EduCourse;
import com.eight.common.pojo.EduSubject;
import com.eight.common.pojo.EduTeacher;
import com.eight.common.pojo.*;
import com.eight.common.utils.OssUtil;
import com.eight.common.vo.Result;
import com.eight.qianTeacher.service.EduChapterService;
import com.eight.qianTeacher.service.EduCourseDescriptionService;
import com.eight.qianTeacher.service.EduCourseService;
import com.eight.qianTeacher.service.QianTeacherService;
import com.eight.qianTeacher.vo.ChapterVo;
import com.eight.qianTeacher.vo.ObjVo;
import com.eight.qianTeacher.vo.SubjectVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * @program: qy8-education
 * @description: QianTeacherController
 * @author: 韩宗耀
 * @create: 2023-06-02 15:57
 **/
@RestController
@RequestMapping("/qianTeacher")
@Api("客户端教师模块")
public class QianTeacherController {
    @Resource
    private QianTeacherService qianTeacherService;
    @Autowired
    private EduChapterService eduChapterService;
    @Resource
    private EduCourseService eduCourseService;
    @Resource
    private EduCourseDescriptionService eduCourseDescriptionService;

    @GetMapping("/findTeacherByMobile/{mobile}")
    @ApiOperation("根据手机号查询教师信息")
    public Result findTeacherByMobile(@PathVariable String mobile) {
        EduTeacher teacherByMobile = qianTeacherService.findTeacherByMobile(mobile);
        return new Result(200, "查询成功", teacherByMobile);
    }

    @PostMapping("/uploadMsg")
    @ApiOperation("上传讲师头像")
    public String uploadMsg(@PathParam("file") MultipartFile file) {
        return OssUtil.uploadFile(file);
    }

    @PostMapping("/updateTeacherByMobile")
    @ApiOperation("更新讲师信息")
    public Result updateTeacherByMobile(@RequestBody EduTeacher eduTeacher) {
        qianTeacherService.updateTeacherByMobile(eduTeacher);
        return Result.success();
    }

    @GetMapping("/getCourseByTeacherPhone/{pageCurrent}/{pageSize}/{mobile}/{checkStatus}")
    @ApiOperation("查询录播信息/分页")
    public Result getCourseByTeacherPhone(
            @PathVariable Integer pageCurrent,
            @PathVariable Integer pageSize,
            @PathVariable String mobile,
            @PathVariable String checkStatus
    ) {
        IPage<EduCourse> courseByTeacherPhone = qianTeacherService.getCourseByTeacherPhone(pageCurrent, pageSize, mobile, checkStatus);
        return Result.success(courseByTeacherPhone);
    }

    @PostMapping("/changeStatus")
    @ApiOperation("课程上下架")
    public Result changeStatus(@RequestBody EduCourse eduCourse) {
        qianTeacherService.changeStatus(eduCourse);
        return Result.success();
    }

    @PostMapping("/findSubjectByStatus/{pageCurrent}/{pageSize}")
    @ApiOperation("课程信息查询/分页")
    public Result findSubjectByStatus(
            @PathVariable Integer pageCurrent,
            @PathVariable Integer pageSize,
            @RequestBody SubjectVo subjectVo
    ) {
        IPage<EduSubject> subjectById = qianTeacherService.findSubjectById(pageCurrent, pageSize, subjectVo);
        return Result.success(subjectById);
    }


    @GetMapping("/findPidById/{pid}")
    @ApiOperation("查询课程分类")
    public Result findPidById(@PathVariable String pid) {
        List<EduSubject> pidById = qianTeacherService.findPidById(pid);
        return Result.success(pidById);
    }

    @PostMapping("/updateCourseSubject")
    @ApiOperation("修改课程")
    public  Result updateCourseSubject(@RequestBody ObjVo objVo){

        if(StringUtils.hasText(objVo.getId())){
            eduCourseDescriptionService.updateDes(objVo.getId(),objVo.getDescription());
            eduCourseService.updateCourse(objVo);
            return Result.success();
        }else{
            String updateCourseSubject = qianTeacherService.updateCourseSubject(objVo);
            return Result.success(updateCourseSubject);
        }

    }


    @PostMapping("chapter/findChapterById/{courseId}")
    @ApiOperation("初始化课程章节")
    public Result findChapterById(@PathVariable String courseId) {
        List<EduChapter> all = qianTeacherService.findChapterById(courseId);
        return Result.success(all);
    }

    @PostMapping("chapter/insertChapterWithCourseId")
    @ApiOperation("添加章节名称")
    public Result insertChapterWithCourseId(@RequestBody EduChapter eduChapter) {
        List<EduChapter> eduChapters = eduChapterService.insertChapter(eduChapter);
        return Result.success(eduChapters);
    }

    @PostMapping("/deleteChapterWithCourseId")
    @ApiOperation("删除章节")
    public Result deleteChapterWithCourseId(@RequestBody EduChapter eduChapter) {
        qianTeacherService.deleteChapterWithCourseId(eduChapter);
        return Result.success();
    }

    @GetMapping("/findAllVideo/{chapterId}")
    @ApiOperation("查询所有课程视频")
    public Result findAllVideo(@PathVariable String chapterId) {
        List<EduVideo> allVideo = qianTeacherService.findAllVideo(chapterId);
        return Result.success(allVideo);
    }

    @PostMapping("/insertVideo")
    @ApiOperation("新增课程视频")
    public Result insertVideo(@RequestBody EduVideo eduVideo) {
        qianTeacherService.insertVideo(eduVideo);
        return Result.success();
    }

    @PostMapping("/updateVideo")
    @ApiOperation("更新课程视频信息")
    public Result updateVideo(@RequestBody EduVideo eduVideo) {
        qianTeacherService.updateVideo(eduVideo);
        return Result.success();
    }

    @DeleteMapping("/deleteById/{id}/{sort}/{chapterId}")
    @ApiOperation("删除课程视频")
    public Result deleteById(
            @PathVariable String id,
            @PathVariable Integer sort,
            @PathVariable String chapterId
    ) {
        qianTeacherService.deleteById(id, sort, chapterId);
        return Result.success();
    }
}
